package session;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.plealog.genericapp.api.EZEnvironment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色相关session
 */
public class RoleSession extends BaseSession implements Serializable {
	/**
	 * dds文件所在目录
	 */
	@PersistentData
	public static String DDS_PATH;
	/**
	 * xml文件打包后的文件名，不含后缀
	 */
	@PersistentData
	public static String XML_FILENAME = "LG_M_8200"; //默认狼的配置文件

	@PersistentData
	public static String MATERIAL_PATH;

	@PersistentData
	public static List<String[]> TEXTURE_XML_ITEMS;

	/**
	 * 材质贴图配置，设置24组，每组20个
	 * 这个数据留着退出程序时保存
	 * <p>
	 * Mtd path,[type,suffix]
	 */
	@PersistentData
	public static Map<String, List<String[]>> TEXTURE_JSON_ITEMS;

	static {
		TEXTURE_XML_ITEMS = new ArrayList<>();
		TEXTURE_XML_ITEMS.add(new String[]{"0x00", ""});
		TEXTURE_XML_ITEMS.add(new String[]{"0x01", "D"});
		TEXTURE_XML_ITEMS.add(new String[]{"0x67", "S"});
		TEXTURE_XML_ITEMS.add(new String[]{"0x6A", ""});
		TEXTURE_XML_ITEMS.add(new String[]{"0x6B", "N"});
		for (int i = 0; i < 3; i++) {
			TEXTURE_XML_ITEMS.add(new String[]{"", ""});
		}

		TEXTURE_JSON_ITEMS = new HashMap<>();
		// 一组
		// 默认有漫反射、法线、高光
		ArrayList<String[]> l1 = new ArrayList<>();
		l1.add(new String[]{"g_DiffuseTexture", "D,d"});
		l1.add(new String[]{"g_BumpmapTexture", "N,n"});
		l1.add(new String[]{"g_SpecularTexture", "S,s"});
		for (int i = 0; i < 17; i++)
			l1.add(new String[]{"", ""});
		TEXTURE_JSON_ITEMS.put("M[A]_e.mtd", l1);

		// 二组
		ArrayList<String[]> l2 = new ArrayList<>();
		l2.add(new String[]{"c1300_AMSN_em_snp_Texture2D_2_AlbedoMap_0", "D"});
		l2.add(new String[]{"c1300_AMSN_em_snp_Texture2D_7_NormalMap_4", "N"});
		l2.add(new String[]{"c1300_AMSN_em_snp_Texture2D_1_EmissiveMap", "G"});
		for (int i = 0; i < 17; i++)
			l2.add(new String[]{"", ""});
		TEXTURE_JSON_ITEMS.put("glow_blend.mtd", l2);

		// 再来22组空组
		for (int i = 0; i < 22; i++) {
			ArrayList<String[]> l3 = new ArrayList<>();
			for (int j = 0; j < 20; j++) {
				l3.add(new String[]{"", ""});
			}
			TEXTURE_JSON_ITEMS.put("槽" + i, l3);
		}
	}

	/**
	 * 初始化session数据
	 * TODO 初始化session数据之后再写
	 */
	public static void INIT_SESSION_DATA() {
	}

	public static void READ_SESSION_DATA() {
		if (!BaseSession.getLocalData("DDS_PATH").equals(""))
			DDS_PATH = JSON.parseObject(BaseSession.getLocalData("DDS_PATH"), String.class);
		if (!BaseSession.getLocalData("XML_FILENAME").equals(""))
			XML_FILENAME = JSON.parseObject(BaseSession.getLocalData("XML_FILENAME"), String.class);
		if (!BaseSession.getLocalData("MATERIAL_PATH").equals(""))
			MATERIAL_PATH = JSON.parseObject(BaseSession.getLocalData("MATERIAL_PATH"), String.class);
		if (!BaseSession.getLocalData("TEXTURE_XML_ITEMS").equals(""))
			TEXTURE_XML_ITEMS = JSON.parseArray(BaseSession.getLocalData("TEXTURE_XML_ITEMS"), String[].class);
		if (!BaseSession.getLocalData("TEXTURE_JSON_ITEMS").equals("")) {
			JSONObject mapJson = JSON.parseObject(BaseSession.getLocalData("TEXTURE_JSON_ITEMS"));
			for (String s : mapJson.keySet()) {
				TEXTURE_JSON_ITEMS.put(s, JSON.parseArray(mapJson.get(s).toString(), String[].class));
			}
		}
	}

	public static void SAVE_SESSION_DATA() {
		EZEnvironment.setApplicationProperty("DDS_PATH", JSON.toJSONString(DDS_PATH));
		EZEnvironment.setApplicationProperty("XML_FILENAME", JSON.toJSONString(XML_FILENAME));
		EZEnvironment.setApplicationProperty("MATERIAL_PATH", JSON.toJSONString(MATERIAL_PATH));
		EZEnvironment.setApplicationProperty("TEXTURE_XML_ITEMS", JSON.toJSONString(TEXTURE_XML_ITEMS));
		EZEnvironment.setApplicationProperty("TEXTURE_JSON_ITEMS", JSON.toJSONString(TEXTURE_JSON_ITEMS));
	}
}
